
// pinia 定义公共状态和方法
// 组合式
// 1. defineStore('counter', callback)  组合式写法要求第二参数传入回调函数(setup函数)  => 回调函数的返回值,就是store实例中存储的数据
// 2. 定义数据的方法同Vue3  ref shallowRef reactive shallowReactive computed 

import { ref,reactive,computed } from "vue";
import { defineStore } from "pinia";

export let useDemoStore = defineStore("demo",()=>{
    let a = ref(1);
    let b = ref(2);

    let doubleA = computed(()=>{
        return a.value * 2
    })
    let doubleB = computed(()=>{
        return a.value * 2
    })

    let both = computed(()=>{
        return a.value + b.value;
    })

    let doubleBoth = computed(()=>{
        return doubleA.value + doubleB.value;
    })

    let addA = ()=>{
        a.value++
    }

    let addB = ()=>{
        b.value++
    }


    let addBoth = (payload) => {
        this.addA();
        this.addB();
    }
    let addA_Async = async()=>{
        return new Promise((resolve,reject)=>{
            setTimeout(()=>{
                a.value ++
                resolve();
            },1000);
        })
    }
    let addB_Async = async()=>{
        return new Promise((resolve,reject)=>{
            setTimeout(()=>{
                b.value ++
                resolve();
            },2000);
        })
    }
    let addBoth_Async = async()=>{
        await addA_Async()
        console.log(11111);
        await addB_Async();
        console.log(22222);
    }       



    return {
        a,
        b,
        doubleA,
        doubleB,
        both,
        doubleBoth,
        addA,
        addB,
        addBoth,
        addA_Async,
        addB_Async,
        addBoth_Async
    }
}) 